/**
 * storage 封装
 */
import config from '../config/index'

class Storage {
    setItem(key, val) {
        let storage = this.getStorage()
        storage[key] = val
        window.localStorage.setItem(config.namespace, JSON.stringify(storage))
    }
    getItem(key) {
        return this.getStorage()[key]
    }
    getStorage() {
        let storage = window.localStorage.getItem(config.namespace)
        return storage ? JSON.parse(storage) : {}
    }
    clearItem(key) {
        let storage = this.getStorage()
        delete storage[key]
        window.localStorage.setItem(config.namespace, JSON.stringify(storage))
    }
    clearAll() {
        window.localStorage.setItem(config.namespace, JSON.stringify({}))
    }
}

export default new Storage()
